package com.winit.clQueue.web;

import com.alibaba.fastjson.JSONObject;
import com.winit.clQueue.service.ClQueueService;
import com.winit.common.base.bean.ExecuteResult;
import com.winit.common.base.web.BaseController;
import com.winit.common.rabbitSend.MsgSend;
import com.winit.commons.util.SeqGenerate;
import com.winit.redis.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by wanglin on 2018/6/4.
 */
@Component
@Api(value = "clQueue", tags = {"clQueue"}, description = "clQueue")
@RestController
@RequestMapping("/api/v1/clQueue")
public class ClQueueController {
    @Autowired
    ClQueueService clQueueService;

    @Autowired
    RedisService redisService;

    @Autowired
    MsgSend msgSend;

    @RequestMapping(value = "/getApp", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取应用")
    @ResponseBody
    public ExecuteResult getApp(
            @ApiParam(value = "国家",required=false) @RequestParam(value = "country", required = false) String country ,
            @ApiParam(value = "区域",required=false) @RequestParam(value = "zone", required = false) String zone,
            @ApiParam(value = "类型",required=false) @RequestParam(value = "type", required = false) String type
            ){
        return clQueueService.getApps(country,zone,type);
    }

    @RequestMapping(value = "/getClParam", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取云链参数")
    @ResponseBody
    public ExecuteResult getTest(
            @ApiParam(value = "应用",required=false) @RequestParam(value = "appId", required = false) String appId,
            @ApiParam(value = "id",required=false) @RequestParam(value = "id", required = false) String id,
            @ApiParam(value = "是否查固定参数，是：'1' 否：0 默认为：0",required=false) @RequestParam(value = "isParam", required = false) String isParam
            ){
        return clQueueService.getClParam(appId,id,isParam);
    }

    @RequestMapping(value = "/send", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "cloudLinkSend")
    @ResponseBody
    public ExecuteResult cloudLinkSend(@RequestBody JSONObject jsonObject){
        ExecuteResult executeResult;
        redisService.setObj(jsonObject.get("selfId"),jsonObject);
        executeResult=clQueueService.cloudLinkSend(jsonObject);
        return executeResult;
    }

    @RequestMapping(value = "/getCQNo", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取新任务编号")
    @ResponseBody
    public ExecuteResult getTaskNo(){
        ExecuteResult er=new ExecuteResult();
        er.setData("CQ"+SeqGenerate.createSerialNo());
        return er;
    }


    @RequestMapping(value = "/cloudLinkRtn", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "云链返回调用")
    @ResponseBody
    public void cloudLinkRtn(
            @ApiParam(value = "id",required=false) @RequestParam(value = "id", required = false) String id ,
            @ApiParam(value = "状态",required=false) @RequestParam(value = "status", required = false) String status
    ){
        JSONObject obj =(JSONObject) redisService.getObj(id);
        if(null==obj){
            return;
        }else{
            obj.put("success",status);
            msgSend.sendMsg(obj);
            redisService.delObj(id);
        }
        return ;
    }


    @RequestMapping(value = "/testSend", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "测试发送")
    @ResponseBody
    public void testSend(@ApiParam(value = "id",required=false) @RequestParam(value = "id", required = false) String id){
        JSONObject json =(JSONObject) redisService.getObj(id);
        msgSend.sendMsg(json);
        redisService.del(id);
    }


}
